package com.groupbuying.provider.service.impl;


import com.groupbuying.provider.constants.CommonConstants;
import com.groupbuying.provider.context.FilterContextHandler;
import com.groupbuying.provider.dao.StoreCheckoutCounterRelDao;
import com.groupbuying.provider.domain.StoreCheckoutCounterRelDO;
import com.groupbuying.provider.dto.StoreCheckoutCounterRelDTO;
import com.groupbuying.provider.service.StoreCheckoutCounterRelService;
import com.groupbuying.provider.utils.PageUtils;
import com.groupbuying.provider.utils.Query;
import com.groupbuying.provider.utils.R;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class StoreCheckoutCounterRelServiceImpl implements StoreCheckoutCounterRelService {
    @Autowired
    private StoreCheckoutCounterRelDao storeCheckoutCounterRelDao;

    @Override
    public StoreCheckoutCounterRelDO get(String uuid) {
        return storeCheckoutCounterRelDao.get(uuid);
    }

    @Override
    public List<StoreCheckoutCounterRelDO> list(Map<String, Object> map) {
        return storeCheckoutCounterRelDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return storeCheckoutCounterRelDao.count(map);
    }


    @Override
    public StoreCheckoutCounterRelDO getByAccountUuid(String accountId) {
        Map<String,Object> param = new HashMap<>();
        param.put("delflag", CommonConstants.COMMON_FLAG_UNDEL);
        param.put("staffuuid", accountId);

        List<StoreCheckoutCounterRelDO> list = storeCheckoutCounterRelDao.list(param);
        return list!=null&& list.size()>0?list.get(0): null;
    }


    @Override
    public StoreCheckoutCounterRelDO getByAccountUuidAndCounterUuid(String counterUuid, String accountId ,String delflag) {
        Map<String,Object> param = new HashMap<>();
        param.put("delflag",delflag);
        param.put("counteruuid", counterUuid);
        param.put("staffuuid", accountId);

        List<StoreCheckoutCounterRelDO> list = storeCheckoutCounterRelDao.list(param);
        return list!=null&& list.size()>0?list.get(0): null;
    }



    @Override
    public List<StoreCheckoutCounterRelDO> getListByCounterUuid(String pageShow, String nowPage, String counterUuid) {
        Map relParam = new HashMap();
        relParam.put("counteruuid", counterUuid);
        relParam.put("sort", "createOpeTime");
        if(StringUtils.isNotBlank(nowPage)||StringUtils.isNotBlank(pageShow)){
            relParam.put("offset", Integer.parseInt(nowPage));
            relParam.put("limit", Integer.parseInt(pageShow));
        }
        relParam.put("delflag", CommonConstants.COMMON_FLAG_UNDEL);
        return storeCheckoutCounterRelDao.list(relParam);
    }

    @Override
    public List<StoreCheckoutCounterRelDTO> getListByAccountId(Query query) {
        return storeCheckoutCounterRelDao.getListByAccountId(query);
    }

    @Override
    public int getListCountByAccountId(Query query) {
        return storeCheckoutCounterRelDao.getListCountByAccountId(query);
    }

    @Override
    public R getCheckoutCounterList(Map<String, Object> mapParams) {
        String providerId = FilterContextHandler.getName();
        mapParams.put("providerId", providerId);
        mapParams.put("delflag", CommonConstants.COMMON_FLAG_UNDEL);
        mapParams.put("sort", "counter.createopetime");
        mapParams.put("order", "desc");
        /*Map map = new HashMap();
        map.put("sort", "storeUuid");
        map.put("order", "desc");
        map.put("offset", Integer.parseInt(nowPage));
        map.put("limit", Integer.parseInt(pageShow));
        map.put("delflag", CommonEnum.COMMON_FLAG_UNDEL);
        List<StoreCheckoutCounterDO> storeCheckoutCounterDOList = null;
        if (StringUtils.isNotBlank(chainStoresUuid) && StringUtils.isEmpty(storeUuid)) {
            List<StoreMainInfoDO> list = storeMainInfoService.getAllChildStoreByChainStoresUuid(chainStoresUuid);
            if (list != null && list.size() > 0) {
                List<String> storeUuids = list.stream().map(StoreMainInfoDO::getUuid).collect(Collectors.toList());
                map.put("storeuuids", storeUuids);
                storeCheckoutCounterDOList = storeCheckoutCounterService.queryListInStoreUuid(map);
            }
        }else {
            map.put("storeuuid", storeUuid);
            storeCheckoutCounterDOList = storeCheckoutCounterService.list(map);
        }
        map.put("storeuuid", storeUuid);
        List<StoreCheckoutCounterDO> storeCheckoutCounterDOList = storeCheckoutCounterService.list(map);*/

        Query query = new Query(mapParams);
        List<StoreCheckoutCounterRelDTO> list = getListByAccountId(query);
        int total = getListCountByAccountId(query);

        /*List<StoreCheckoutCounterDO> list = storeCheckoutCounterService.list(query);
        int total = storeCheckoutCounterService.count(query);*/

        PageUtils pageUtil = new PageUtils(list, total);
        return R.ok().put("page", pageUtil);
    }
}
